/**
 *  初始化
 */

//棋盘初始化
let checkerBoard = document.getElementById("CheckerBoard");
let content = checkerBoard.getContext("2d");
let board = new Image();
board.src = "./img/checkerboard.jpeg";

let black = new Image();
black.src = "./img/black.png";

let white = new Image();
white.src = "./img/white.png";

let pointer_white = new Image();
pointer_white.src = "./img/pointer_black.png";

let pointer_black = new Image();
pointer_black.src = "./img/pointer_white.png";

board.onload = function () {
  content.drawImage(board, 0, 0);
};

//按钮初始化
let ready_btn = document.getElementById("ready-btn");
let cancel_ready_btn = document.getElementById("cancel-ready-btn");
let regret_btn = document.getElementById("regret-btn");
let surrender_btn = document.getElementById("surrender-btn");
let draw_btn = document.getElementById("draw-btn");
let message_btn = document.getElementById("send-message-btn");
let reject_draw_btn = document.getElementById("reject-draw-btn");
let agree_draw_btn = document.getElementById("agree-draw-btn");
let regret_btn_confirm = document.getElementById("regret-btn-confirm");
let reject_regret_btn = document.getElementById("reject-regret-btn");
let agree_regret_btn = document.getElementById("agree-regret-btn");
let surrender_btn_confirm = document.getElementById("surrender-btn-confirm");
let draw_btn_confirm = document.getElementById("draw-btn-confirm");
let logout_btn = document.getElementById("logout-btn");
let leave_game_btn = document.getElementById("leave-game-btn");
let personal_information_btn = document.getElementById(
  "personal-information-btn"
);
let save_information_btn = document.getElementById("save-information-btn");

//个人信息初始化
let your_UID = parseInt(Cookies.get("UID"));
let your_avatar = Cookies.get("image_url");
let your_nick = Cookies.get("nick");
let your_gender = Cookies.get("gender");
let your_signature = Cookies.get("signature");
let your_hidden_point = Cookies.get("hidden_Point");
let your_play_num =
  parseInt(Cookies.get("winNum")) +
  parseInt(Cookies.get("loseNum")) +
  parseInt(Cookies.get("drawNum"));
let your_win_rate;
if (your_play_num === 0) {
  your_win_rate = 0;
} else {
  your_win_rate = Math.round(
    100 * (parseInt(Cookies.get("winNum")) / your_play_num)
  );
}

let opponent_UID;
let opponent_avatar;
let opponent_nick;
let opponent_play_num;
let opponent_win_num;
let opponent_lose_num;
let opponent_draw_num;
let opponent_win_rate;

let your_nick_msg = document.getElementById("your-nick-msg");
let your_play_num_msg = document.getElementById("your-play-num-msg");
let your_win_rate_msg = document.getElementById("your-win-rate-msg");

let opponent_nick_msg = document.getElementById("opponent-nick-msg");
let opponent_play_num_msg = document.getElementById("opponent-play-num-msg");
let opponent_win_rate_msg = document.getElementById("opponent-win-rate-msg");

let rid_msg = document.getElementById("rid-msg");
let chess_color_msg = document.getElementById("chess-color-msg");
let chatBox_msg = document.getElementById("chatBox-msg");
let alert_msg = document.getElementById("alert-msg");

let chatBox = document.getElementById("chatBox");
let chatMsg;
let inputChatMsg = document.getElementById("inputChatMsg");
let chat_msg = document.getElementById("chat-msg");
let chatBox_class = inputChatMsg.getAttribute("class");
let chatBox_invalid_class = chatBox_class.concat(" is-invalid");

let inputNick = document.getElementById("inputNick");
let inputSignature = document.getElementById("inputSignature");
let inputPassword1 = document.getElementById("inputPassword1");
let inputPassword2 = document.getElementById("inputPassword2");

let nick_msg = document.getElementById("nick-msg");
let signature_msg = document.getElementById("signature-msg");
let password1_msg = document.getElementById("password1-msg");
let password2_msg = document.getElementById("password2-msg");

let nick_class = inputNick.getAttribute("class");
let signature_class = inputSignature.getAttribute("class");
let password1_class = inputPassword1.getAttribute("class");
let password2_class = inputPassword2.getAttribute("class");

let nick_invalid_class = nick_class.concat(" is-invalid");
let signature_invalid_class = signature_class.concat(" is-invalid");
let password1_invalid_class = password1_class.concat(" is-invalid");
let password2_invalid_class = password2_class.concat(" is-invalid");

let nick_valid_class = nick_class.concat(" is-valid");
let signature_valid_class = signature_class.concat(" is-valid");
let password1_valid_class = password1_class.concat(" is-valid");
let password2_valid_class = password2_class.concat(" is-valid");

//对局控制初始化

// playing 为 0 时表示未开局，为 1 时表示已开局
let playing;

// round 为 0 时表示不是你的回合，为 1 时表示是你的回合
let round;

// chess_color 为 1 时表示你是白棋，为 2 时表示你是黑棋
let chess_color;

//棋盘数据
let chessData = new Array(20);

//棋子计数
let count;

//为悔棋准备的棋子计数
let countData = new Array(20);

//最后一颗棋子的位置
let last_x = 0,
  last_y = 0;

//开局时间
let startTime;

your_nick_msg.innerHTML = your_nick;
your_play_num_msg.innerHTML = your_play_num;
your_win_rate_msg.innerHTML = your_win_rate + "%";
playing = 0;
round = 0;
chess_color = 0;
for (let x = 0; x < 20; x++) {
  chessData[x] = new Array(20);
  countData[x] = new Array(20);
}

//网络请求初始化
let readyAjax;
let base_url = new SockJS("http://test.eslans.com:8000/gobang");
let stompClient = Stomp.over(base_url);

/**
 *  界面监听、绘制等主体操作
 */

//棋盘监听操作
checkerBoard.addEventListener("click", function (event) {
  let rect = checkerBoard.getBoundingClientRect();
  let x = parseInt((event.clientX - rect.left) / 33);
  let y = parseInt((event.clientY - rect.top) / 33);
  if (playing === 1 && round === 1 && chessData[x][y] === 0) {
    draw(checkerBoard, event);
  }
});

//画棋子
function draw(canvas, event) {
  let rect = canvas.getBoundingClientRect();
  let x = parseInt((event.clientX - rect.left) / 33);
  let y = parseInt((event.clientY - rect.top) / 33);
  for (let i = 0; i < 15; i++) {
    for (let j = 0; j < 15; j++) {
      if (chessData[i][j] == 1) {
        content.drawImage(white, i * 33 + 2, j * 33 + 2);
      } else if (chessData[i][j] == 2) {
        content.drawImage(black, i * 33 + 2, j * 33 + 2);
      }
    }
  }
  if (chessData[x][y] === 0) {
    //黑棋
    if (chess_color === 2) {
      content.drawImage(black, x * 33 + 2, y * 33 + 2);
      content.drawImage(pointer_white, x * 33 + 12, y * 33 + 11);
      chessData[x][y] = 2;
    }
    //白棋
    else {
      content.drawImage(white, x * 33 + 2, y * 33 + 2);
      content.drawImage(pointer_black, x * 33 + 12, y * 33 + 11);
      chessData[x][y] = 1;
    }
    count++;
    countData[x][y] = count;
    round = 0;
    sendChess(x, y);
  }
}

//找到最后下的一步棋的坐标
function findLastChess() {
  for (let i = 0; i < 15; i++) {
    for (let j = 0; j < 15; j++) {
      if (countData[i][j] === count) {
        last_x = i;
        last_y = j;
        return;
      }
    }
  }
}

//准备，开始匹配
ready_btn.onclick = function () {
  //重置棋盘
  checkerBoard.width = checkerBoard.width;
  content.drawImage(board, 0, 0);
  for (let x = 0; x < 15; x++) {
    for (let y = 0; y < 15; y++) {
      chessData[x][y] = 0;
      countData[x][y] = 0;
    }
  }

  opponent_nick_msg.innerHTML =
    "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
  opponent_play_num_msg.innerHTML = "请先准备";
  opponent_win_rate_msg.innerHTML =
    "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
  chess_color_msg.innerHTML = "请先准备";
  rid_msg.innerHTML = "";

  readyAjax = $.ajax({
    type: "GET",
    url: "http://test.eslans.com:8000/user/ready",
    data: {
      UID: your_UID,
      hidden_Point: your_hidden_point,
    },
    success: function (result) {
      if (result.succ) {
        $("#ready").modal("hide");
        playing = 1;
        count = 0;
        chess_color = result.data.chessColor;
        if (chess_color === 2) {
          chess_color_msg.innerHTML = "你执黑棋";
          round = 1;
        } else {
          chess_color_msg.innerHTML = "你执白棋";
          round = 0;
        }
        opponent_UID = result.data.player.uid;
        opponent_nick = result.data.player.name;
        opponent_win_num = result.data.player.winNumber;
        opponent_lose_num = result.data.player.loseNumber;
        opponent_draw_num = result.data.player.drawNumber;
        opponent_play_num =
          opponent_win_num + opponent_lose_num + opponent_draw_num;
        if (opponent_play_num === 0) {
          opponent_win_rate = 0;
        } else {
          opponent_win_rate = Math.round(
            100 * (result.data.player.winNumber / opponent_play_num)
          );
        }
        opponent_nick_msg.innerHTML = opponent_nick;
        opponent_play_num_msg.innerHTML = opponent_play_num;
        opponent_win_rate_msg.innerHTML = opponent_win_rate + "%";
        rid_msg.innerHTML = "房间号：" + result.data.rid;
        Cookies.set("RID", result.data.rid);
        startTime = getTime();
        ready_btn.setAttribute("disabled", "disabled");
        regret_btn.removeAttribute("disabled");
        surrender_btn.removeAttribute("disabled");
        draw_btn.removeAttribute("disabled");
        commonUtil.message("匹配成功，对局开始", "success");
      }
    },
  });
};

//取消匹配
cancel_ready_btn.onclick = function () {
  $("#ready").modal("hide");
  readyAjax.abort();

  $.ajax({
    type: "GET",
    url: "http://test.eslans.com:8000/user/cancel",
    data: {
      player: your_UID,
    },
  });
};

//退出登录
logout_btn.onclick = function () {
  if (playing === 1) {
    $("#playing-leave").modal("show");
  } else {
    logout();
  }
};

//对局中，确认离开游戏
leave_game_btn.onclick = function () {
  sendLeaveMsg();
  logout();
};

//拒绝悔棋
reject_regret_btn.onclick = function () {
  sendOp(9);
  commonUtil.message("已拒绝对方的悔棋");
};

//同意悔棋
agree_regret_btn.onclick = function () {
  findLastChess();
  sendRegretChess(last_x, last_y);
  regretModify();
  round = 0;
  commonUtil.message("已同意对方的悔棋");
};

//判断能够悔棋的时刻
regret_btn.onclick = function () {
  if (round === 0) {
    if (count > 0) {
      $("#regret").modal("show");
    } else {
      commonUtil.message("现在不能悔棋哦");
    }
  } else {
    commonUtil.message("现在不能悔棋哦");
  }
};

//悔棋
regret_btn_confirm.onclick = function () {
  commonUtil.message("已向对方请求悔棋", "primary");
  sendOp(7);
};

//悔棋后重新绘图，重置棋盘数据
function regretModify() {
  chessData[last_x][last_y] = 0;
  countData[last_x][last_y] = 0;
  count--;
  findLastChess();
  checkerBoard.width = checkerBoard.width;
  content.drawImage(board, 0, 0);
  for (let i = 0; i < 15; i++) {
    for (let j = 0; j < 15; j++) {
      if (i === last_x && j === last_y) {
        if (chessData[i][j] == 1) {
          if (countData[i][j] !== 0) {
            content.drawImage(white, i * 33 + 2, j * 33 + 2);
            content.drawImage(pointer_black, i * 33 + 12, j * 33 + 11);
          }
        } else if (chessData[i][j] == 2) {
          if (countData[i][j] !== 0) {
            content.drawImage(black, i * 33 + 2, j * 33 + 2);
            content.drawImage(pointer_white, i * 33 + 12, j * 33 + 11);
          }
        }
      } else {
        if (chessData[i][j] == 1) {
          if (countData[i][j] !== 0) {
            content.drawImage(white, i * 33 + 2, j * 33 + 2);
          }
        } else if (chessData[i][j] == 2) {
          if (countData[i][j] !== 0) {
            content.drawImage(black, i * 33 + 2, j * 33 + 2);
          }
        }
      }
    }
  }
}

//认输
surrender_btn_confirm.onclick = function () {
  sendOp(1);
};

//求和
draw_btn_confirm.onclick = function () {
  commonUtil.message("已向对方请求和棋", "primary");
  sendOp(2);
};

//同意和棋
agree_draw_btn.onclick = function () {
  sendOp(5);
  Cookies.set("drawNum", parseInt(Cookies.get("drawNum")) + 1);
  opponent_play_num++;
  endGame();
  rid_msg.innerHTML = "本局已和棋";
  commonUtil.message("您已同意和棋", "primary");
};

//拒绝和棋
reject_draw_btn.onclick = function () {
  sendOp(6);
  commonUtil.message("您已拒绝和棋");
};

//聊天框
function focusChatInput() {
  inputChatMsg.setAttribute("class", chatBox_class);
}

//回车发送消息
$("body").keypress(function (event) {
  if (event.which === 13) {
    message_btn.click();
  }
});

//点击按钮发送消息
message_btn.onclick = function () {
  chatMsg = inputChatMsg.value;

  if (playing === 0) {
    inputChatMsg.setAttribute("class", chatBox_invalid_class);
    chat_msg.innerHTML = "对局开始时才能发消息哦";
  } else if (chatMsg === "") {
    inputChatMsg.setAttribute("class", chatBox_invalid_class);
    chat_msg.innerHTML = "消息不能为空";
  } else {
    chatMsg = your_nick + "  " + getTime() + "\n" + chatMsg + "\n";
    sendMsg(chatMsg);
    chatBox.append(chatMsg);
    chatBox.scrollTop = chatBox.scrollHeight;
    inputChatMsg.value = "";
  }
};

//展示目前的个人信息
personal_information_btn.onclick = function () {
  inputNick.setAttribute("value", your_nick);
  inputSignature.setAttribute("value", your_signature);
  if (your_gender === "男") {
    $("#inputGender input:radio[value='男']").prop("checked", true);
  } else if (your_gender === "女") {
    $("#inputGender input:radio[value='女']").prop("checked", true);
  } else {
    $("#inputGender input:radio[value='保密']").prop("checked", true);
  }
  $("#personal-information").modal("show");
};

//保存修改的信息
save_information_btn.onclick = function () {
  if (inputNick.value === "") {
    inputNick.setAttribute("class", nick_invalid_class);
    nick_msg.innerHTML = "请设置您的昵称";
  } else if (
    inputPassword1.value !== "" &&
    inputPassword1.value !== inputPassword2.value
  ) {
    inputNick.setAttribute("class", nick_valid_class);
    inputSignature.setAttribute("class", signature_valid_class);
    inputPassword1.setAttribute("class", password1_invalid_class);
    inputPassword2.setAttribute("class", password2_invalid_class);
    password2_msg.innerHTML = "两次密码不一致";
  } else {
    saveInfo();
    focusInput();
    $("#personal-information").modal("hide");
    commonUtil.message("信息修改成功", "success");
  }
};

function focusInput() {
  inputNick.setAttribute("class", nick_class);
  inputSignature.setAttribute("class", signature_class);
  inputPassword1.setAttribute("class", password1_class);
  inputPassword2.setAttribute("class", password2_class);
}

//对局结束划线
function drawWinLine(x, y, color) {
  let a = 0, b = 0;
  content.lineWidth = 5;
  //判断横
  for(a = x - 4, b = y; a <= x + 4; a++) {
    if(a < 0) {
      a = 0;
    }
    if(chessData[a][y] === color && chessData[a+1][y] === color && chessData[a+2][y] === color && chessData[a+3][y] === color && chessData[a+4][y] === color) {
      content.beginPath();
      content.moveTo(a * 33 + 18, b * 33 +17);
      content.lineTo((a+4) * 33 + 18, b * 33 + 17);
      content.strokeStyle = "#B30505"; 
      content.stroke();
      content.closePath();
      return;
    }
  }
  //判断竖
  for(b = y - 4; b <= y + 4; b++) {
    if(b < 0) {
      b = 0;
    }
    if(chessData[x][b] === color && chessData[x][b+1] === color && chessData[x][b+2] === color && chessData[x][b+3] === color && chessData[x][b+4] === color) {
      content.beginPath();
      content.moveTo(x * 33 + 18, b * 33 + 17);
      content.lineTo(x * 33 + 18, (b+4) * 33 + 17);
      content.strokeStyle = "#B30505"; 
      content.stroke();
      content.closePath();
      return;
    }
  }
  //判断右斜
  for(a = x - 4, b = y - 4; a <= x + 4; a++, b++) {
    if(a < 0) {
      a = 0;
    }
    if(b < 0) {
      b = 0;
    }
    if(chessData[a][b] === color && chessData[a+1][b+1] === color && chessData[a+2][b+2] === color && chessData[a+3][b+3] === color && chessData[a+4][b+4] === color) {
      content.beginPath();
      content.moveTo(a * 33 + 18, b * 33 + 17);
      content.lineTo((a+4) * 33 + 18, (b+4) * 33 + 17);
      content.strokeStyle = "#B30505"; 
      content.stroke();
      content.closePath();
      return;
    }
  }
  //判断左斜
  for(a = x - 4, b = y + 4; a <= x + 4; a++, b--) {
    if(a < 0) {
      a = 0;
    }
    if(b < 0) {
      b = 0;
    }
    if(chessData[a][b] === color && chessData[a+1][b-1] === color && chessData[a+2][b-2] === color && chessData[a+3][b-3] === color && chessData[a+4][b-4] === color) {
      content.beginPath();
      content.moveTo(a * 33 + 18, b * 33 + 17);
      content.lineTo((a+4) * 33 + 18, (b-4) * 33 + 17);
      content.strokeStyle = "#B30505"; 
      content.stroke();
      content.closePath();
      return;
    }
  }
}

//游戏结束，修改界面展示的数据
function endGame() {
  your_play_num =
    parseInt(Cookies.get("winNum")) +
    parseInt(Cookies.get("loseNum")) +
    parseInt(Cookies.get("drawNum"));
  your_win_rate = Math.round(
    100 * (parseInt(Cookies.get("winNum")) / your_play_num)
  );
  your_play_num_msg.innerHTML = your_play_num;
  your_win_rate_msg.innerHTML = your_win_rate + "%";
  opponent_play_num = opponent_win_num + opponent_lose_num + opponent_draw_num;
  opponent_win_rate = Math.round(100 * (opponent_win_num / opponent_play_num));
  opponent_play_num_msg.innerHTML = opponent_play_num;
  opponent_win_rate_msg.innerHTML = opponent_win_rate + "%";
  playing = 0;
  round = 0;
  chess_color = 0;
  ready_btn.removeAttribute("disabled");
  regret_btn.setAttribute("disabled", "disabled");
  surrender_btn.setAttribute("disabled", "disabled");
  draw_btn.setAttribute("disabled", "disabled");
}

//退出登录
function logout() {
  Cookies.remove("UID");
  Cookies.remove("nick");
  Cookies.remove("image_url");
  Cookies.remove("gender");
  Cookies.remove("signature");
  Cookies.remove("hidden_Point");
  Cookies.remove("winNum");
  Cookies.remove("loseNum");
  Cookies.remove("drawNum");
  window.location.href = "login.html";
}

/**
 *  网络请求部分
 */

//发送聊天消息
function sendMsg(content) {
  stompClient.send(
    "/app/chat",
    {},
    JSON.stringify({
      content: content,
      ownUID: parseInt(your_UID),
      enemyUID: parseInt(opponent_UID),
    })
  );
}

//发送操作消息
function sendOp(operation) {
  stompClient.send(
    "/app/operation",
    {},
    JSON.stringify({
      RID: parseInt(Cookies.get("RID")),
      ownUID: parseInt(your_UID),
      enemyUID: parseInt(opponent_UID),
      startTime: startTime,
      endTime: getTime(),
      operation: operation,
    })
  );
}

//发送悔棋消息
function sendRegretChess(x, y) {
  stompClient.send(
    "/app/operation",
    {},
    JSON.stringify({
      RID: parseInt(Cookies.get("RID")),
      ownUID: parseInt(your_UID),
      enemyUID: parseInt(opponent_UID),
      startTime: startTime,
      endTime: getTime(),
      operation: 8,
      x: x + 1,
      y: y + 1,
    })
  );
}

//发送退出房间消息
function sendLeaveMsg() {
  stompClient.send(
    "/app/close",
    {},
    JSON.stringify({
      RID: parseInt(Cookies.get("RID")),
      ownUID: parseInt(your_UID),
      enemyUID: parseInt(opponent_UID),
    })
  );
}

//发送棋子消息
function sendChess(x, y) {
  stompClient.send(
    "/app/chessboard",
    {},
    JSON.stringify({
      x: x + 1,
      y: y + 1,
      RID: parseInt(Cookies.get("RID")),
      ownUID: parseInt(your_UID),
      enemyUID: parseInt(opponent_UID),
      startTime: startTime,
      endTime: getTime(),
    })
  );
}

//接收消息
stompClient.connect({}, function () {
  //接收棋子信息
  stompClient.subscribe("/user/chessboard/" + your_UID, function (msg) {
    let message = JSON.parse(msg.body);

    //如果这个棋子是对方的，则绘制棋子并判断是否结束
    if (message.chessOwnerUID === opponent_UID) {
      let opponent_x = message.x - 1;
      let opponent_y = message.y - 1;
      for (let i = 0; i < 15; i++) {
        for (let j = 0; j < 15; j++) {
          if (chessData[i][j] == 1) {
            content.drawImage(white, i * 33 + 2, j * 33 + 2);
          } else if (chessData[i][j] == 2) {
            content.drawImage(black, i * 33 + 2, j * 33 + 2);
          }
        }
      }

      //如果你是黑棋，则绘制对手的白棋
      if (chess_color === 2) {
        content.drawImage(white, opponent_x * 33 + 2, opponent_y * 33 + 2);
        content.drawImage(
          pointer_black,
          opponent_x * 33 + 12,
          opponent_y * 33 + 11
        );
        chessData[opponent_x][opponent_y] = 1;
      }
      //如果你是白棋，则绘制对手的黑棋
      else {
        content.drawImage(black, opponent_x * 33 + 2, opponent_y * 33 + 2);
        content.drawImage(
          pointer_white,
          opponent_x * 33 + 12,
          opponent_y * 33 + 11
        );
        chessData[opponent_x][opponent_y] = 2;
      }
      count++;
      countData[opponent_x][opponent_y] = count;

      if (message.status === 0) {
        if(chess_color === 1) {
          drawWinLine(opponent_x, opponent_y, 2);
        } else {
          drawWinLine(opponent_x, opponent_y, 1);
        }
        Cookies.set("loseNum", parseInt(Cookies.get("loseNum")) + 1);
        opponent_win_num++;
        opponent_play_num++;
        endGame();
        rid_msg.innerHTML = "你被打败了";
        commonUtil.message("五子连珠，你被打败了");
      } else {
        round = 1;
      }
    }
    //如果这个棋子是自己的，则游戏结束，你获得胜利
    else if (message.chessOwnerUID === your_UID) {
      let your_x = message.x - 1;
      let your_y = message.y - 1;
      if (message.status === 0) {
        if(chess_color === 1) {
          drawWinLine(your_x, your_y, 1);
        } else {
          drawWinLine(your_x, your_y, 2);
        }
        Cookies.set("winNum", parseInt(Cookies.get("winNum")) + 1);
        opponent_lose_num++;
        opponent_play_num++;
        endGame();
        rid_msg.innerHTML = "你获得胜利";
        commonUtil.message("五子连珠，你获得胜利", "success");
      }
    }
  });

  //接收聊天信息
  stompClient.subscribe("/user/chat/" + your_UID, function (msg) {
    let message = JSON.parse(msg.body);

    if (message.enemyUID === opponent_UID) {
      chatBox.append(message.content);
      chatBox.scrollTop = chatBox.scrollHeight;
    }
  });

  //接收操作消息
  stompClient.subscribe("/user/operation/" + your_UID, function (msg) {
    let message = JSON.parse(msg.body);

    //认输
    if (message.result === 1) {
      sendOp(3);
      Cookies.set("winNum", parseInt(Cookies.get("winNum")) + 1);
      opponent_lose_num++;
      opponent_play_num++;
      endGame();
      rid_msg.innerHTML = "你获得胜利";
      commonUtil.message("对方投降，你获得胜利", "success");
    }
    //求和
    else if (message.result === 2) {
      $("#opponent-draw").modal("show");
    }
    //同意认输
    else if (message.result === 3) {
      Cookies.set("loseNum", parseInt(Cookies.get("loseNum")) + 1);
      opponent_win_num++;
      opponent_play_num++;
      endGame();
      rid_msg.innerHTML = "您已认输";
      commonUtil.message("您已认输");
    }
    //同意和棋
    else if (message.result === 5) {
      Cookies.set("drawNum", parseInt(Cookies.get("drawNum")) + 1);
      opponent_play_num++;
      endGame();
      rid_msg.innerHTML = "本局已和棋";
    }
    //拒绝和棋
    else if (message.result === 6) {
      commonUtil.message("对方拒绝和棋");
    }
    //请求悔棋
    else if (message.result === 7) {
      $("#opponent-regret").modal("show");
    }
    //同意悔棋
    else if (message.result === 8) {
      findLastChess();
      regretModify();
      round = 1;
      commonUtil.message("对方已同意您的悔棋", "success");
    }
    //拒绝悔棋
    else if (message.result === 9) {
      commonUtil.message("对方拒绝你的悔棋");
    }
  });

  //对手离开了房间
  stompClient.subscribe("/user/close/" + your_UID, function () {
    Cookies.set("winNum", parseInt(Cookies.get("winNum")) + 1);
    opponent_lose_num++;
    opponent_play_num++;
    endGame();
    rid_msg.innerHTML = "你获得胜利";
    commonUtil.message("对方离开了房间，你获得胜利", "success");
  });
});

//保存修改的个人信息
function saveInfo() {
  if (inputNick.value !== your_nick) {
    $.ajax({
      type: "POST",
      url: "http://test.eslans.com:8000/user/update",
      data: {
        UID: your_UID,
        key: "name",
        value: inputNick.value,
      },
      success: function (result) {
        if (result.succ) {
          Cookies.set("nick", inputNick.value);
          your_nick_msg.innerHTML = inputNick.value;
        } else {
          commonUtil.message("昵称修改失败");
        }
      },
    });
  } else if (inputPassword1.value !== "") {
    $.ajax({
      type: "POST",
      url: "http://test.eslans.com:8000/user/update",
      data: {
        UID: your_UID,
        key: "password",
        value: inputPassword1.value,
      },
      success: function (result) {
        if (!result.succ) {
          commonUtil.message("密码修改失败");
        }
      },
    });
  } else if (
    $('#inputGender input[name="gender"]:checked ').val() !== your_gender
  ) {
    $.ajax({
      type: "POST",
      url: "http://test.eslans.com:8000/user/update",
      data: {
        UID: your_UID,
        key: "gender",
        value: $('#inputGender input[name="gender"]:checked ').val(),
      },
      success: function (result) {
        if (!result.succ) {
          commonUtil.message("性别修改失败");
        }
      },
    });
  } else if (inputSignature.value !== your_signature) {
    $.ajax({
      type: "POST",
      url: "http://test.eslans.com:8000/user/update",
      data: {
        UID: your_UID,
        key: "signature",
        value: inputSignature.value,
      },
      success: function (result) {
        if (!result.succ) {
          commonUtil.message("个性签名修改失败");
        }
      },
    });
  }
}
